
package view;

import java.awt.Dimension;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import model.Conference;
import model.Database;
import model.Paper;
import model.User;

/**
 * This class represents the main frame used in the program.
 * 
 * @author Greg Coleman, Cody Thompson
 * @author Rylie Nelson
 */
public class MainFrame extends JFrame {
	
	/** The database. */
	private Database dbase = new Database();
	
	/** The main panel. */
	private MainPanel mainPanel;
	
	/** The login data. */
	private Login login;
	
	/** The reviewer selection panel. */
	private ReviewerSelection reviewerSelection;
	
	/** The SPC selection panel. */
	private SPCSelection spcSelection;
	
	/** The selected conference. */
	private Conference selectedConf;
	
	/** The user. */
	private User me;
	
	/** The access level of the user. 0 = author, 1 = reviewer, 2 = pc, 3 = spc. */
	private int accessLevel;
	
	/**
	 * This class supports the various JPanels this program will use.
	 * @author Gregory Coleman and Team BigOhFace
	 */
	public MainFrame() {
		setTitle("Conference Review System");
		
		FileInputStream fileIn;
		try {
			fileIn = new FileInputStream("somedata.ser");
	        ObjectInputStream in = new ObjectInputStream(fileIn);
	        dbase = (Database) in.readObject();
	        in.close();
	        fileIn.close();
		} catch (FileNotFoundException e) {
			try {
				fileIn = new FileInputStream("somedata_default.ser");
		        ObjectInputStream in = new ObjectInputStream(fileIn);
		        dbase = (Database) in.readObject();
		        in.close();
		        fileIn.close();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		login = new Login(dbase,this);
		add(login);
		setResizable(false);
		pack();
		setLocationRelativeTo(null);
	}
	
	/**
	 * Launches the main panel.
	 * @param al the access level.
	 * @param cn the conference.
	 * @param u the user.
	 */
	public void launchMain(int al, Conference cn, User u) {
		accessLevel = al;
		selectedConf = cn;
		me = u;
		mainPanel = new MainPanel(dbase,this);
		remove(login);
		login.setVisible(false);
		add(mainPanel);
		setResizable(true);
		setMinimumSize(new Dimension(800,600));
		pack();
		setLocationRelativeTo(null);
	}
	
	public void redrawMainPanel() {
		remove(mainPanel);
		mainPanel = new MainPanel(dbase,this);
		add(mainPanel);
		setResizable(true);
		setMinimumSize(new Dimension(800,600));
		pack();
		setLocationRelativeTo(null);
	}
	
	/**
	 * Launches the reviewer selection panel.
	 * @param papel the paper to add reviewers too.
	 */

	public void launchRevSel(Paper papel) {
		reviewerSelection = new ReviewerSelection(selectedConf,papel);
		reviewerSelection.setDefaultCloseOperation(reviewerSelection.DISPOSE_ON_CLOSE);
		reviewerSelection.setVisible(true);
	}
	
	public void launchSPCSel(Paper papel) {
		spcSelection = new SPCSelection(selectedConf,papel);
		spcSelection.setDefaultCloseOperation(spcSelection.DISPOSE_ON_CLOSE);
		spcSelection.setVisible(true);
	}
	
	/**
	 * Launches the paper.
	 */
	public void launchPaper() {
		
	}
	
	/**
	 * Returns the conference.
	 * @return the conference.
	 */
	public Conference getConf() {
		return selectedConf;
	}
	
	/**
	 * Returns the user.
	 * @return the user.
	 */
	public User getUser() {
		return me;
	}
	
	/**
	 * Returns the access level.
	 * @return the access level.
	 */
	public int getAccess() {
		return accessLevel;
	}
}
